package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.RandomCodeGenerator;
import cc.linker.steplink.crm.dao.CrmHelperDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class CrmHelperImpl {

    @Autowired
    private CrmHelperDao crmHelperDao;

    @Value("${casclient.clientHost}")
    private String host;

    public List<Map<String,Object>> list(Map<String, Object> record) {
        List<Map<String,Object>> result =  crmHelperDao.list(record);
        result.forEach(x->{
            String labelNames="";
            if(x.get("labelIds")!=null){
                String labelIds = x.get("labelIds").toString();
                labelNames = crmHelperDao.getLabelNamesByIds(labelIds);
            }
            x.put("labelNames",labelNames);
            x.put("labelLinkUrl",host+"/crmhelper/trans?id="+x.get("id").toString());

        });
        return result;
    }

    public Map<String,Object> get(String id) {
        Map<String,Object> result = crmHelperDao.get(id);
        if(result!=null && result.get("labelIds")!=null){
            result.put("labelNames",crmHelperDao.getLabelNamesByIds(result.get("labelIds").toString()));
            result.put("labelLinkUrl",host+"/crmhelper/trans?id="+id);
        }
        return  result;
    }

    public void del(Map<String, Object> params) {
        crmHelperDao.del(params);
    }

    public void add(Map<String, Object> params) throws Exception {
        if (StringUtils.isEmpty(params.get("id"))){
            String id = UUID.randomUUID().toString();
            params.put("id",id);
            crmHelperDao.add(params);
        }else{
            crmHelperDao.edit(params);
        }
    }

    public String getTokenByOrgId(String orgId) {
        return crmHelperDao.getTokenByOrgId(orgId);
    }

    public String addCustomer(Map userInfo) {

        String openId = userInfo.get("openid").toString();
        String token = userInfo.get("token").toString();
        String orgId = userInfo.get("orgId").toString();
//        String customerId = crmHelperDao.getCustomerIdByOpenIdAndToken(openId,token);
        String customerId = crmHelperDao.getCustomerIdByOpenIdAndOrgId(openId,orgId);
        if(customerId!=null){
            return customerId;
        }
        //生成customerId
        Map<String,Object> map = new HashMap<>();
        customerId = RandomCodeGenerator.getCustomerId(Constants.CUSTOMER_CODE_SOURCE_WX);
        map.put("customerId",customerId);
        map.put("customerName",userInfo.get("nickname"));
        map.put("customerSourceChannel",2);
        map.put("customerAvatar",userInfo.get("headimgurl"));
        map.put("createUser","CRM助手");
        map.put("orgId",userInfo.get("orgId"));
        map.put("wxOpenId",openId);
        map.put("wxToken",token);
        crmHelperDao.insertCustomer(map);

        Map<String,Object> temp = new HashMap();
        temp.put("customerInfoId",UUID.randomUUID().toString());
        temp.put("customerId",customerId);
        crmHelperDao.insertCustomerInfo(temp);


        Map<String,Object> params = new HashMap();
        params.put("customerId",customerId);
        params.put("customerName",userInfo.get("nickname"));
        params.put("wxGender",userInfo.get("sex"));
        params.put("wxAddress",userInfo.get("country").toString()+"@"+userInfo.get("province").toString()+"@"+userInfo.get("city").toString());
        params.put("wxOpenId",openId);
        params.put("wxToken",token);
        params.put("wxAvatar",userInfo.get("headimgurl"));
        crmHelperDao.insertCustomerWx(params);

        return customerId;
    }

    public void addLabels(String label,String customerId) {
        String[] labelIds =  label.split(",");
        Set<String> labels = new HashSet<>();
        Collections.addAll(labels, labelIds);

        crmHelperDao.addLabels(labels,customerId);
    }

}
